//
//  PhotosWithUserIDViewController.m
//  FlickrSearching
//
//  Created by HOCVIEN on 4/25/15.
//  Copyright (c) 2015 HOCVIEN. All rights reserved.
//

#import "PhotosWithUserIDViewController.h"
#import "PhotosWithUserIDTableViewCell.h"
#import "FlickrHelper.h"
@interface PhotosWithUserIDViewController ()
@property __block NSArray* array;
@property NSString* userID;
@property __block NSMutableArray* searchArray;
@end

@implementation PhotosWithUserIDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.array = [NSMutableArray array];
    self.searchArray = [NSMutableArray array];
    // Do any additional setup after loading the view.
    NSString* getUser = self.userID;
    if (self.userID == nil) {
        getUser = @"91873576%40N08";
    }
    
    [FlickrHelper getPhotosSetWithUserId:getUser callback:^(NSArray *result) {
        self.array = result;
        
        self.searchArray = [NSMutableArray arrayWithArray:self.array];
        [self.imgListTbl reloadData];
    }];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    self.searchArray = [NSMutableArray array];
    if (searchText.length == 0) {
        self.searchArray = [NSMutableArray arrayWithArray:self.array];
    }
    else{
        for (int i=0 ; i<self.array.count;i++) {
            NSDictionary* dic = [self.array objectAtIndex:i];
            if ([((NSString*)[dic objectForKey:@"title"])rangeOfString:searchText].length>0) {
                [self.searchArray addObject:dic];
            }
        }
    }

    [self.imgListTbl reloadData];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.searchArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString* indentifier = @"PhotosWithUserIDTableViewCell";
    PhotosWithUserIDTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        [tableView registerClass:[PhotosWithUserIDTableViewCell class] forCellReuseIdentifier:indentifier];
        cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    }
    
    if (self.searchArray) {
        cell.titleLb.text = [((NSDictionary*)[self.searchArray objectAtIndex:indexPath.row]) valueForKey:@"title"];
        cell.numOfPhotosLb.text = [((NSDictionary*)[self.searchArray objectAtIndex:indexPath.row]) valueForKey:@"server"];
        dispatch_async(dispatch_queue_create("thread", 0), ^{
            UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[((NSDictionary*)[self.array objectAtIndex:indexPath.row]) valueForKey:@"url_t"]]]];
            dispatch_async(dispatch_get_main_queue(), ^{
                cell.img.image = image ;
            });
        });
        
        
        
        
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
